/**
 * @fileOverview
 * @name circle-node.js
 * @author lidh04
 * @license copyright to shc
 */

import gateway from "./circle-gateway.js";
import { buildUrl, getData, postData } from "./circle-common.js";

/// <reference path="./circle-types.js" />

/**
 * subscribe api
 * @returns {Promise<HeartBeatPOResponse>} - the subscribe response.
 */
export async function subscribe() {
  const url = buildUrl(gateway.node.path.subscribe);
  return getData(url, +gateway.http.timeoutRead);
}

/**
 * server features.
 * @returns {Promise<BaseNodeInfoPOResponse>} - the response.
 */
export async function serverFeatures() {
  const url = buildUrl(gateway.node.path.serverFeatures);
  return getData(url, +gateway.http.timeoutRead);
}

/**
 * @typedef {Object} VinScriptSignature
 * @property {string} asm - the asm
 * @property {string} hex - the hex string
 */

/**
 * @typedef {Object} VIn
 * @property {string} txid - the transaction id
 * @property {number} vout - the output index of the transaction `txid`
 * @property {VinScriptSignature} scriptSig - the script signature
 * @property {string[]} txinwitness - the tx in witness.
 * @property {number} sequence - the sequence
 * @property {string[]} addresses - the address list
 * @property {string} value - the value of the input
 */

/**
 * @typedef {Object} ScriptPubKey
 * @property {string} asm - the asm
 * @property {string} hex - the hex
 * @property {string} type - the type
 * @property {string[]} addresses - the address list
 */

/**
 * @typedef {Object} VOut
 * @property {string} value - the output value
 * @property {number} n - the output index
 * @property {ScriptPubKey} scriptPubKey - the script signed to the pub key
 */

/**
 * broadcast Transaction
 * @param {Object} input - user input.
 * @param {string} input.txid - the transaction id
 * @param {number} input.type - the transaction type: 0 currency, 1 ownership, 2 identity
 * @param {string} input.hash - the transaction hash
 * @param {number} input.version - the block version
 * @param {number} input.size - the transaction size
 * @param {number} input.vsize - the transaction vsize
 * @param {number} input.weight - the weight
 * @param {number} input.locktime - the lock time
 * @param {VIn[]} input.vin - the inputs
 * @param {VOut[]} input.vout - the outputs
 * @param {string} input.blockHash - the block hash
 * @param {number} input.confirmations - the confirmations
 * @param {number} input.time - the timestamp for transaction
 * @param {number} input.blocktime - the block timestamp for the transaction
 * @returns {Promise<StringResponse>} - the response.
 */
export async function broadcastTransaction(input) {
  const url = buildUrl(gateway.node.path.broadcastTransaction);
  return postData(url, input, +gateway.http.timeoutWrite);
}
